package towerDefense.towers;

import java.awt.image.BufferedImage;
import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.SpriteGroup;
import com.golden.gamedev.object.Timer;
import towerDefense.projectiles.ProjectileSprite;
import towerDefense.projectiles.ProjectileQualities;


public class TowerSprite extends TargettingSprite implements Cloneable
{

    private ProjectileQualities myProjectile;
    private Timer myTimer;
    private int myCost;


    public TowerSprite (BufferedImage image,
                        double x,
                        double y,
                        double r,
                        Sprite s,
                        ProjectileQualities p,
                        int ss,
                        int cost)
    {
        super(image, x, y, r, 0, s);
        buildTower(p, ss, cost);
    }


    public TowerSprite (BufferedImage image,
                        double r,
                        Sprite s,
                        ProjectileQualities p,
                        int ss,
                        int cost)
    {
        super(image, r, 0, s);
        buildTower(p, ss, cost);
    }


    public TowerSprite (double x,
                        double y,
                        double r,
                        Sprite s,
                        ProjectileQualities p,
                        int ss,
                        int cost)
    {
        super(x, y, r, s);
        buildTower(p, ss, cost);
    }


    public TowerSprite (double r, ProjectileQualities p, int ss, int cost)
    {
        super(r, null);
        buildTower(p, ss, cost);
    }


    public TowerSprite (Sprite s, ProjectileQualities p, int ss, int cost)
    {
        super(s);
        buildTower(p, ss, cost);
    }


    public TowerSprite (BufferedImage image,
                        double r,
                        int ss,
                        ProjectileQualities p,
                        int cost)
    {
        super(image, r, 0, null);
        buildTower(p, ss, cost);
    }


    /**
     * @param p
     * @param ss
     */
    private void buildTower (ProjectileQualities p, int ss, int cost)
    {
        setProjectile(p);
        setShootSpeed(ss);
        setCost(cost);
    }


    public void setProjectile (ProjectileQualities p)
    {
        this.myProjectile = p;
    }


    public ProjectileQualities getProjectile ()
    {
        return myProjectile;
    }


    public ProjectileSprite fireProjectile ()
    {
        return new ProjectileSprite(myTarget,
                                    this.getCenterX(),
                                    this.getCenterY(),
                                    myProjectile);
    }


    public void setShootSpeed (int ss)
    {
        myTimer = new Timer(ss);
    }


    public int getShootSpeed ()
    {
        return (int) myTimer.getDelay();
    }


    public void update (long l, SpriteGroup proj)
    {
        super.update(l);
        proj.removeInactiveSprites();

        if (myTarget != null)
        {
            if (!myTarget.isActive() ||
                !this.getCircle().intersects(myTarget.getX(),
                                             myTarget.getY(),
                                             myTarget.getWidth(),
                                             myTarget.getHeight()))
            {
                myTarget = null;
                return;
            }

            if (myTimer.action(l))
            {
                proj.add(this.fireProjectile());
                myTimer.refresh();
            }
        }

    }


    @Override
    public Object clone () throws CloneNotSupportedException
    {
        return new TowerSprite(this.getImage(),
                               this.getRadius(),
                               this.getShootSpeed(),
                               this.getProjectile(),
                               this.getCost());
    }


    public void setCost (int myCost)
    {
        this.myCost = myCost;
    }


    public int getCost ()
    {
        return myCost;
    }

}
